<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    
    <head>
        <title>编辑角色</title>
        <header th:replace="sys/head::html"></header>
    </head>
    <body>
        <form class="layui-form">
            <input type="hidden" th:value="${sysRole.id}" id="roleId" name="id">
            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>角色名
                </label>
                <div class="layui-input-inline">
                    <input type="text" th:value="${sysRole.name}" id="name" name="name" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="desc" class="layui-form-label">
                    <span class="x-red">*</span>描述
                </label>
                <div class="layui-input-inline">
                    <input type="text" th:value="${sysRole.description}" id="description" name="description" required=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>
          <div class="layui-form-item">
              <label class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="edit" lay-submit="">
                  提交
              </button>
          </div>
      </form>

    <script>layui.use(['form', 'layer', 'laydate'],
        function() {
            $ = layui.jquery;
            var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate;

            //自定义验证规则
            form.verify({
                name: function(value) {
                    if (value.length < 5) {
                        return '昵称至少得5个字符啊';
                    }
                }
            });

            //监听提交
            form.on('submit(edit)',
            function(data) {
               $.ajax({
                   url: appPath + '/sys/role/update',
                   type: 'post',
                   data: data.field,
                   dataType: 'json',
                   success: function (result) {
                        if(result.code == 200){
                            layer.alert("更新成功", {
                                    icon: 6
                                },
                                function() {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        }else{
                            layer.alert(result.msg);
                        }
                   }
               })

                return false;
            });

        });
        function getAllRole(callback) {
            $.ajax({
                url: appPath + '/sys/role/all',
                type: 'get',
                dataType: 'json',
                success: function (result) {
                    var html = '';
                    if(result.datas && result.datas.length > 0) {
                        for(i in result.datas) {
                            var data = result.datas[i];
                            html += '<input type="radio" name="roleId" value="'+data.id+'" title="'+data.name+'" >' +
                                    '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-main layui-icon"></i><div>'+data.name+'</div></div>'
                        }
                        $('.show-role-container').html(html);
                        getCurrentUserRole(callback);
                    }
                }
            });
        }
        function getCurrentUserRole(callback) {
            var userId = $('#userId').val();
            $.ajax({
                url: '/userRole/getRoleUserByUserId',
                type: 'get',
                data: {userId: userId},
                dataType: 'json',
                success: function (result) {
                   if(result.code == 200){
                       if(result.data) {
                           $('[name=roleId]').each(function () {
                                if($(this).val() == result.data.roleId){
                                    $(this).attr('checked', 'checked');
                                    return false;
                                }
                           })
                       }
                   }
                    if(callback){
                        callback();
                    }
                }
            });

        }
    </script>

    </body>

</html>
